﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Content;

using BeachRunner.Tileengine;

namespace BeachRunner.Tileengine
{
    /// <summary>
    /// Klasse zum laden der Kartendaten
    /// 
    /// Autor: OLD (Mattis Hänel)
    /// </summary>
    public class MapDataReader : ContentTypeReader<MapData>
    {
        /// <summary>
        /// Läd Kartendaten
        /// </summary>
        /// <param name="input">Eine Referenz auf die Kartendaten</param>
        /// <param name="existingInstance"></param>
        /// <returns>Ein Objekt welches die Kartendaten enthält.</returns>
        protected override MapData Read(ContentReader input,
                                           MapData existingInstance)
        {
            // Hole die Kartenausmaße
            int width = input.ReadInt32();
            int height = input.ReadInt32();

            // Hole den Dateinamen des Tilesets
            string tileset = input.ReadString();

            // Hole die Anzahl der Ebenen
            int numOfLayers = input.ReadInt32();

            // Hole die Namen der Ebenen
            Dictionary<String, int> nameToLayer = new Dictionary<string, int>();
            for (int i = 0; i < numOfLayers; i++)
            {
                nameToLayer.Add(input.ReadString(), i);
            }

            // Hole alle Layer
            short[][] layerData = new short[numOfLayers][];
            for (int layer = 0; layer < numOfLayers; layer++)
            {
                layerData[layer] = new short[width * height];
                for (int i = 0; i < width * height; i++)
                {
                    layerData[layer][i] = input.ReadInt16();
                }
            }

            // Erstelle das MapData Objekt und gebe dieses zurück
            MapData mapData = new MapData(width, height, tileset, layerData, nameToLayer);
            return mapData;
        }
    }
}
